package com.tanjun.userservice.web.biz.admin;

import cn.hutool.core.collection.CollectionUtil;
import com.tanjun.common.utils.ConvertUntil;
import com.tanjun.userservice.apiservice.request.dept.DeptIdListRequest;
import com.tanjun.userservice.apiservice.request.dept.DeptRequest;
import com.tanjun.userservice.apiservice.response.admindept.admin.DeptResponse;
import com.tanjun.userservice.apiservice.response.admindept.admin.DeptTreeResponse;
import com.tanjun.userservice.web.service.user.entity.BaseAdminDeptEntity;
import com.tanjun.userservice.web.service.user.BaseAdminDeptService;
import com.tanjun.web.service.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminDeptBiz {
    @Autowired
    private BaseAdminDeptService adminDeptService;

    /**
     * 根据id获取部门信息
     *
     */
    public DeptResponse getById(Long id) {
        BaseAdminDeptEntity dept= adminDeptService.getById(id);
        return ConvertUntil.convert(dept, DeptResponse.class);
    }

    /**
     * 新增部门
     *
     * @param deptRequest
     */
    public void addDept(DeptRequest deptRequest) {
        BaseAdminDeptEntity dept= ConvertUntil.convert(deptRequest, BaseAdminDeptEntity.class);
        dept.setCreator(LoginUserUtil.getAdminOperate());
        adminDeptService.addDept(dept);
    }

    /**
     * 更新部门
     *
     * @param deptRequest
     */
    public void updateDept(DeptRequest deptRequest) {
        BaseAdminDeptEntity dept= ConvertUntil.convert(deptRequest, BaseAdminDeptEntity.class);
        dept.setCreator(LoginUserUtil.getAdminOperate());
        adminDeptService.updateDept(dept);
    }

    /**
     * 删除部门
     *
     * @param paramVo
     */

    @Transactional(rollbackFor = Exception.class)
    public void deleteDepList(DeptIdListRequest paramVo) {
        paramVo.getDeptIds().forEach(adminDeptService::delDept);
    }

    /**
     * 返回部门树形结构
     *
     * @return 返回树形结构
     */
    public List<DeptTreeResponse> tree() {
        List<BaseAdminDeptEntity> listAll= adminDeptService.listAll();
        List<DeptTreeResponse> listRe=new ArrayList<>();
        toTree(listAll,listRe,null);
        return listRe;
    }

    /**
     * 生成部门树形结构
     * @param listAll 所有的部门数据
     * @param listRe 返回结果
     * @param dto 当前操作的dto,第一层的时候为null，
     */
    private void toTree(List<BaseAdminDeptEntity> listAll, List<DeptTreeResponse> listRe,
                        DeptTreeResponse dto){
        Long parentId=dto==null?0L:dto.getId();
        List<BaseAdminDeptEntity> list=listAll
                .stream()
                .filter(t-> Objects.equals(t.getParentId(),parentId))
                .sorted(Comparator.comparingInt(BaseAdminDeptEntity::getSort))
                .collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(list)){
            List<DeptTreeResponse> listChildren=ConvertUntil.convert(list, DeptTreeResponse.class);
            if(dto==null){
                listRe.addAll(listChildren);
            }
            else {
                dto.setChildren(listChildren);
            }
            listChildren.forEach(d->{
                toTree(listAll,listRe,d);
            });
        }
    }



}
